package socket;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

/**
 * 多线程
 * **/
public class ThreadPoolTcpClient {

    public static void main(String[] args) throws  Exception{
        System.out.println("tcp服务器启动....");
        ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
        while(true){
            final ServerSocket server = new ServerSocket(8080);
            newCachedThreadPool.execute(new Runnable() {
                public void run() {
                    try{
                        Socket socket = server.accept();
                        InputStream inputStream = socket.getInputStream();
                        byte[] bytes = new byte[1024];
                        int len = inputStream.read(bytes);
                        String content = new String(bytes,0,len,"GBK");
                        System.out.println(content);
                    }catch (Exception e){
                        e.printStackTrace();
                    }finally {
                        try {
                            server.close();
                        } catch (IOException e) {
                        }
                    }
                }
            });
        }
    }
}
